// -----------------------------------------------------------------------
//  <copyright file="ResourceController.cs" company="LiuliuSoft">
//      Copyright (c) 2022-2025 DaprPlus. All rights reserved.
//  </copyright>
//  <site>https://dapr.plus</site>
//  <last-editor>郭明锋</last-editor>
//  <last-date>2025-02-21 14:02</last-date>
// -----------------------------------------------------------------------

using System.ComponentModel;

using DaprPlus.AspNetCore.Mvc;
using DaprPlus.AspNetCore.Mvc.Filters;
using DaprPlus.Authorization.Application.Commands;
using DaprPlus.Authorization.Application.Queries;
using DaprPlus.Authorization.Domain.Entities;

using Microsoft.AspNetCore.Mvc;


namespace DaprPlus.Authorization.Controllers;

[ApiResultWrapperFilter]
public abstract class ResourceControllerBase(IMediator mediator) : ApiControllerBase
{
    [HttpGet("{id}")]
    [Produces<ResourceOutDto>]
    public virtual async Task<IActionResult> Get(long id)
    {
        var query = new GetResourceByIdQuery(id);
        var result = await mediator.Send(query);
        if (result == null)
        {
            return NotFound();
        }

        return Ok(result);
    }

    /// <summary>
    /// 获取用于分配角色权限的实体资源列表
    /// </summary>
    /// <param name="roleId">角色ID</param>
    /// <returns>实体资源树</returns>
    [HttpGet("node/{roleId}")]
    [Produces<List<ResourceNode>>]
    public virtual async Task<IActionResult> GetRoleResourceNode(long roleId)
    {
        var query = new GetRoleResourceNodeQuery { RoleId = roleId };
        var result = await mediator.Send(query);
        return Ok(result);
    }

    [HttpPost("read")]
    [Produces<PageResult<ResourceOutDto>>]
    public virtual async Task<IActionResult> Read([FromBody] GetResourcePageQuery query)
    {
        var page = await mediator.Send(query);
        return Ok(page);
    }

    [HttpPost]
    [Produces<ApiResult>]
    public virtual async Task<IActionResult> Create([FromBody] CreateResourceCommand command)
    {
        var result = await mediator.Send(command);
        return Ok(result);
    }

    [HttpPut]
    [Produces<ApiResult>]
    public virtual async Task<IActionResult> Update([FromBody] UpdateResourceCommand command)
    {
        var result = await mediator.Send(command);
        return Ok(result);
    }

    [HttpDelete]
    [Produces<ApiResult>]
    public virtual async Task<IActionResult> Delete(long[] ids)
    {
        var command = new DeleteResourceCommand(ids);
        var result = await mediator.Send(command);
        return Ok(result);
    }

}
